package com.ilas.service.impl;

import com.ilas.dao.TenantDao;
import com.ilas.entity.Tenant;
import com.ilas.service.TenantService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;

/**
 * <p>${Description}</p>
 *
 * @author luzck
 * @date 2021/3/29 15:09
 */
@Service
public class TenantServiceImpl implements TenantService {
    @Resource
    private TenantDao tenantDao;

    @Resource
    private DataSource master;

    @Override
    public List<Tenant> getList() {
        return tenantDao.findList();
    }

    @Override
    public Tenant add(Tenant tenant) {
        /*try {
            Connection conn = master.getConnection();
            if (conn != null) {
                Statement stm = conn.createStatement();
                // 组织创建数据库sql
                StringBuilder sb = new StringBuilder("CREATE DATABASE ");
                sb.append(tenant.getTenantName()).append(";");

                //创建数据库
                int i = stm.executeUpdate(sb.toString());
                if (i > 0) {
                    System.out.println("创建成功！");
                    //打开 转向数据库
                    sb.setLength(0);
                    sb.append("USE ").append(tenant.getTenantName()).append(";");
                    stm.execute(sb.toString());
                    //执行脚本
                    System.out.println("开始导入数据！");
                    ScriptSql.exeSQLSript(tenant.getTenantName());
                    System.out.println("数据导入完成！");

                    MultipartDataSource multipartDataSource = new MultipartDataSource();
                    multipartDataSource.addDataSource(tenant.getTenantName(),ScriptSql.create(tenant));

                    tenant.setCreateDate(new Date());
                    tenant.setModifyDate(new Date());
                    int flag = tenantDao.insert(tenant);
                    if (flag > 0) {
                        return tenant;
                    }
                } else {
                    System.out.println("创建失败！");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        return null;
    }
}
